<?php
/**
 * Created by PhpStorm.
 * User: Lihong
 * Date: 2017/8/26
 * Time: 10:45
 */

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use common\models\User;
use backend\models\UserForm;
use backend\models\UserExtends;
use backend\models\ImgsForm;
use backend\models\Files;

class UserController extends BaseController{

    public $nodeName = '用户系统';
    public $init_upload_image = '/common/uploads/system/upload.jpg';

    public function init(){
        parent::init();
    }

    public function actionIndex(){

    }

    /**
     *
     */
    public function actionLists(){
        //'extends.user_id','extends.nick_name','user.id','user.email'
        $_lists = UserExtends::find();
        $_lists->select(['*']);
        $_lists->from(UserExtends::tableName() . ' as extends');
        //$_lists->where(['extends.user_id' => ]);
        $_lists->joinWith('user as user' , false, 'LEFT JOIN');

        return $this->render('lists', [
            'lists' => $_lists->orderBy('extends.user_id DESC')->limit(5)->asArray()->all()
        ]);
    }

    public function actionCreate(){
        $model = new UserForm();
        $filesInput = new ImgsForm();
        $filesInput->scenario = 'upload';
        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                //更新user_extends
                $user_extends = new UserExtends();
                $user_extends->setScenario('create');
                $user_extends->user_id = $user->id;
                if($user_extends->save()){
                    $this->session->setFlash('success', '创建成功!');
                }
            }
        }

        return $this->render('data-form', [
            'pageTitle' => '创建账户',
            'model' => $model,
        ]);
    }

    public function actionEdit(){
        $id = $this->request->get('id');
        $user_extends = UserExtends::findOne($id);
        if(!$user_extends){
            $this->session->setFlash('error', '保存失败!');
            return $this->refresh();
        }

        $user_extends->setScenario('update');
        if($this->request->isPost && $user_extends->load($this->request->post())){
            if($user_extends->save()){
                $this->session->setFlash('success', '保存成功!');
            }
        }
        if($user_extends->photo_id <= 0){
            $user_extends->photo_id = 1;
        }
        $result = [
            'model' => $user_extends,
            'pageTitle' => '更新用户',
        ];

        return $this->render('extends-form', $result);
    }
    public function actionDel(){
        $id = $this->request->get('id');

        $user_extends = UserExtends::findOne($id);
        if(!$user_extends){
            $this->session->setFlash('error', '参数错误!');
            return $this->refresh();
        }
        $db = Yii::$app->db;
        $transaction = $db->beginTransaction();  //开启事务
        try {
            if($user_extends->delete()){
                if(!isset($user_extends->user) || !$user_extends->user->delete()){
                    $error = '用户错误!';
                    $this->session->setFlash('error', $error);
                    throw new \Exception($error);//抛出异常
                }
                $this->session->setFlash('success', '删除成功!');
                $transaction->commit();//提交
                return $this->goBack();
            }else{
                $error = array_values($user_extends->getFirstErrors())[0];
                $this->session->setFlash('error', $error);
                throw new \Exception($error);//抛出异常
            }
        }catch (\Exception $e){
            //回滚
            $transaction->rollBack();
            return $this->goBack();
        }
    }


    public function getImageUrl($param){

        $urlList = array();
        if(is_array($param)){
            $files = Files::find()->select(['path', 'name'])->where(['in', 'id', $param])->asArray()->all();

            if(!empty($files) && is_array($files)){
                foreach ($files as $value) {
                    $urlList[] = '/' . trim($value['path'], '/') . '/' . $value['name'];
                }
                return $urlList;
            }
        }

        return false;
    }
}